package com.keyinong.caizhengting.myfragement;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.keyinong.caizhengting.R;
import com.keyinong.caizhengting.tools.HttpTools;
import com.keyinong.caizhengting.adapter.TeamdetailAdapter;
import com.keyinong.caizhengting.entity.TeamdetailEntity;
import com.loopj.android.http.JsonHttpResponseHandler;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * 我的团队详情
 */

public class TeamdetailActivity extends Activity {

    /**
     * 返回
     */
    ImageButton ib_fanhui;
    /**
     * 团队名
     */
    TextView tv_teamname;
    /**
     * 公司名
     */
    TextView tv_cmp;
    /**
     * 负责人
     */
    TextView tv_leader;
    /**
     * 获得数据
     */
    ArrayList<TeamdetailEntity> infolist;
    HttpTools httptools = null;
    String token = "";
    private TeamdetailAdapter teamdetailAdapter;
    /**
     * 团队成员
     */
    private ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_teamdetail);
        SharedPreferences sp = getSharedPreferences("spReg",
                Context.MODE_PRIVATE);
        token = sp.getString("data", "");
        lv = (ListView) findViewById(R.id.lv_chengyuan);
        tv_teamname = (TextView) findViewById(R.id.tv_teamname2);
        tv_cmp = (TextView) findViewById(R.id.tv_cmp2);
        tv_leader = (TextView) findViewById(R.id.tv_leader2);
        Myteamdetail();
        Myteamdetail2();
        ib_fanhui = (ImageButton) findViewById(R.id.ib_fanhui7);
        ib_fanhui.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();

            }
        });
    }

    private void Myteamdetail() {
        infolist = new ArrayList<TeamdetailEntity>();
        String teamid = getIntent().getStringExtra("teamid");
        Log.i("shi", "teamid" + teamid);
        if (httptools == null) {
            httptools = new HttpTools(getApplicationContext());
        }
        httptools.Teamdetail(token, teamid, new JsonTeamdetail());

    }

    private void Myteamdetail2() {
        infolist = new ArrayList<TeamdetailEntity>();
        String teamid = getIntent().getStringExtra("teamid");
        Log.i("shi", "teamid" + teamid);
        if (httptools == null) {
            httptools = new HttpTools(getApplicationContext());
        }
        httptools.Teamdetail(token, teamid, new JsonTeamdetail2());

    }

    public class JsonTeamdetail extends JsonHttpResponseHandler {
        @Override
        public void onSuccess(int statusCode, Header[] headers,
                              JSONObject response) {
            // TODO Auto-generated method stub
            super.onSuccess(statusCode, headers, response);
            try {
                Log.i("zq", "okokokok==" + response.toString());
                boolean code = response.getBoolean("code");
                String msg = response.getString("msg");
                if (code == true) {
                    JSONObject object = response.getJSONObject("data");
                    String name = object.getString("name");
                    String cname = object.getString("cname");
                    String uname = object.getString("uname");
//					JSONArray array = object.getJSONArray("members");
//					for(int i=0;i<array.length();i++){
//						JSONObject members = array.getJSONObject(i);
//						String name1 = members.getString("name");
//						String phone = members.getString("phone");
//						infolist.add(new TeamdetailEntity(name1));
//					}
                    tv_teamname.setText(name);
                    if (cname.equals("null")) {
                        tv_cmp.setText("");
                    } else {
                        tv_cmp.setText(cname);
                    }
                    tv_leader.setText(uname);
//					teamdetailAdapter = new TeamdetailAdapter(getApplicationContext(), infolist);
//					lv.setAdapter(teamdetailAdapter);
//					lv.setDivider(null);
                } else {
                    Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    public class JsonTeamdetail2 extends JsonHttpResponseHandler {
        @Override
        public void onSuccess(int statusCode, Header[] headers,
                              JSONObject response) {
            // TODO Auto-generated method stub
            super.onSuccess(statusCode, headers, response);
            try {
//				Log.i("zq","okokokok==" +response.toString());
                boolean code = response.getBoolean("code");
                String msg = response.getString("msg");
                if (code == true) {
                    JSONObject object = response.getJSONObject("data");
                    JSONArray array = object.getJSONArray("members");
                    Log.i("shi", "members--" + object.toString());
                    for (int i = 0; i < array.length(); i++) {
                        JSONObject members = array.getJSONObject(i);
                        Log.i("shi", "array--" + array.length());
                        String name1 = members.getString("name");
//						String phone = members.getString("phone");
                        infolist.add(new TeamdetailEntity(name1));
                    }
                    teamdetailAdapter = new TeamdetailAdapter(getApplicationContext(), infolist);
                    lv.setAdapter(teamdetailAdapter);
                    lv.setDivider(null);
                } else {
                    Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }


}
